package com.aoscript.kit.ofd2jpg;

import org.ofdrw.layout.OFDDoc;
import org.ofdrw.layout.VirtualPage;
import org.ofdrw.layout.edit.Attachment;
import org.ofdrw.layout.element.Position;
import org.ofdrw.layout.element.canvas.Canvas;
import org.ofdrw.tool.merge.OFDMerger;

import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * Hello world!
 */
public class App {
	public static void main(String[] args)throws Exception {
		Path targetPath=Path.of("d:/300m-10.ofd");
		try(OFDDoc ofdDoc=new OFDDoc(targetPath)){
			var pageLayout = ofdDoc.getPageLayout();
			for (int i = 0; i < 10; i++) {
				VirtualPage vPage = new VirtualPage(ofdDoc.getPageLayout());

				Canvas canvas=new Canvas(pageLayout.getWidth(),pageLayout.getHeight());
				canvas.setPosition(Position.Absolute)
						.setX(0d).setY(0d)
				;
				int finalI = i;

				canvas.setDrawer(ctx->{
					ctx.fillStyle = "rgba(255, 0, 0, 0.8)";
					ctx.font = "20px 宋体";
					ctx.fillText(String.valueOf(finalI),50,50);
				});
				vPage.add(canvas);
				ofdDoc.addVPage(vPage);
			}

			ofdDoc.addAttachment(createAttachment(0));
		}
	}

	private static Attachment createAttachment(int i) {
		Attachment attachment=new Attachment("200m",Path.of("d:/200m"));
		return attachment;
	}

	public static void main1(String[] args)throws Exception {
		int times=4;
		// 1. 提供合并文件输出位置。
		Path dst = Paths.get("d:/dst"+times+".ofd");
		// 3. 创建合并对象
		try (OFDMerger ofdMerger = new OFDMerger(dst)) {
			for (int i = 0; i < times; i++) {
				ofdMerger.add(Paths.get("d:/f"+i+".ofd"));
			}

		}
	}
}
